package com.hzsd.zengbanyi.biaodan_text.utils;

	import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

	public class CheckHttpUtil {
	        
	        public static boolean isNetworkAvailable(Activity mActivity){
	                Context context = mActivity.getApplicationContext();
	                
	                ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(
	                                Context.CONNECTIVITY_SERVICE);
	                
	                if(connectivity == null){
	                        return false;
	                }else {
	                        NetworkInfo[] info = connectivity.getAllNetworkInfo();
	                        if(info != null ){
	                                for(int i=0; i<info.length; i++){
	                                        if(info[i].getState() == NetworkInfo.State.CONNECTED){
	                                                return true;
	                                        }
	                                }
	                        }
	                }
	                return false;
	        }
	        
	        public static void checkhttp(final Activity activity){
	                if(!isNetworkAvailable(activity)){
	                        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
	                        builder.setTitle("网络错误，请检查网络设置");
	                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
	                                
	                                @Override
	                                public void onClick(DialogInterface dialog, int which) {
	                                        // TODO Auto-generated method stub
//	                                activity.finish();
	                                }
	                        });
	                        builder.show();
	                }else {
	                }
	        }
	        public static boolean checkURLIsConnect(final Activity activity){
	        	  if(!isNetworkAvailable(activity)){
                      AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                      builder.setTitle("网络错误，请检查网络设置");
                      builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                              
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                            	  Intent intent = null;
                                  /**
                                   * 判断手机系统的版本！如果API大于10 就是3.0+
                                   * 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
                                   */
                                  if (android.os.Build.VERSION.SDK_INT > 10) {
                                          intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                                  } else {
                                          intent = new Intent();
                                          ComponentName component = new ComponentName(
                                                          "com.android.settings",
                                                          "com.android.settings.WirelessSettings");
                                          intent.setComponent(component);
                                          intent.setAction("android.intent.action.VIEW");
                                  }
                                  activity.startActivity(intent);
                              }
                      });
                      builder.show();
                      return false;
              }else {
              return true;
              }
	        	}

	        public static boolean checkURL(String url){
	        	boolean value=false;
	        	try {
	        	HttpURLConnection conn=(HttpURLConnection)new URL(url).openConnection();
	        	int code=conn.getResponseCode();
	        	
	        	if(code!=200){
	        	value=false;
	        	}else{
	        	value=true;
	        	}
	        	} catch (MalformedURLException e) {
	        	// TODO Auto-generated catch block
	        	e.printStackTrace();
	        	} catch (IOException e) {
	        	// TODO Auto-generated catch block
	        	e.printStackTrace();
	        	}
	        	return value;
	        	}

	}

